package com.example.pieces;

import android.content.Intent;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.pieces.util.MyApplication;
import com.example.pieces.view.ChessboardView;

import org.json.JSONException;
import org.json.JSONObject;

import io.socket.client.Ack;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class MainActivity extends AppCompatActivity {
    private static final String TAG ="MainActivity" ;
    public static String status="0";
    public static Socket mSocket;
    public static String player;
    ChessboardView cbv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cbv= new ChessboardView(this);
        MyApplication app = (MyApplication) getApplication();
        mSocket = app.getSocket();
        mSocket.connect();
        setContentView(R.layout.activity_main);
        mSocket.on("turn",turn);
        Intent i=getIntent();
        player=i.getStringExtra("player");
        if (player==null) {
            player="player1";
        }else{
            player="player2";
        }
    }

    public static void anxia(int validX, int validY) {
        mSocket.emit("turn", validY, validX, new Ack() {
            @Override
            public void call(Object... args) {

            }
        });
    }
    /**
     * 服务器发给客户端，判定棋子是否合法。
     */
    private Emitter.Listener turn = new Emitter.Listener() {

        @Override
        public void call(final Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        JSONObject json=new JSONObject(args[0].toString());
                        status=json.getString("status");//状态，0游戏中，1，刚下子乙方胜利，2，平局
                        int x= Integer.parseInt(json.getString("x"));
                        int y= Integer.parseInt(json.getString("y"));
                        final Point point=new Point(x,y);
                        if (status.equals("1")) {
                            Toast.makeText(MainActivity.this, "棋局结束,请返回大厅！", Toast.LENGTH_SHORT).show();
                                    cbv.setPointToArray(point);
                        }else{
                                    cbv.setPointToArray(point);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    };
    /**
     * 返回大厅
     * @param view
     */
    public void fanhui(View view) {
        if (status.equals("0")) {
            Toast.makeText(MainActivity.this, "游戏中，请勿退出！", Toast.LENGTH_SHORT).show();
        }else{
            Intent i=new Intent(MainActivity.this,ListViewActivity.class);
            startActivity(i);
            finish();
        }
    }

    /**
     * 强制退出
     * @param view
     */
    public void back(View view) {
        Toast.makeText(MainActivity.this, "强制退出", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        cbv.clearArr();
        super.onDestroy();
    }
}
